package org.ligson.cg.folder.init;

import org.ligson.cg.orm.utils.TemplateEngine;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by ligso on 2016/12/17.
 */
public class CreateApp {
    private static Logger logger = LoggerFactory.getLogger(CreateApp.class);

    private File destDir;
    private String packageName;
    private String serviceName;

    private File serviceDir;
    private String packagePath;

    private void createFolders(File root, String[] subDirs) {
        for (String subDir : subDirs) {
            File file = new File(root, subDir);
            if (!file.exists()) {
                boolean flag = file.mkdirs();
                logger.info("初始化目录{}{}", file.getAbsolutePath(), flag ? "成功" : "失败");
            }

        }
    }

    private void initFolders() {
        if (!serviceDir.exists()) {
            serviceDir.mkdirs();
        }
        //File serviceDir = new File(root, serviceName);
        createFolders(destDir, new String[]{serviceName + "/src/main/resources",
                serviceName + "/src/test/java",
                serviceName + "/src/test/resources",
                serviceName + "/src/main/java/" + packagePath + "/entity",
                serviceName + "/src/main/java/" + packagePath + "/dao/impl",
                serviceName + "/src/main/java/" + packagePath + "/service/impl"});

    }

    private void copyXml(Map<String, Object> map) {
        TemplateEngine.write("folder/xml/ReuseSQL.ftl", map, new File(serviceDir, "src/main/resources/META-INF/mybatis/ReuseSQL.xml"));
        TemplateEngine.write("folder/xml/mybatis-config.ftl", map, new File(serviceDir, "src/main/resources/META-INF/mybatis/mybatis-config.xml"));
    }

    private void copyGradle(Map<String, Object> map) {
        TemplateEngine.write("folder/gradle/buildRoot.ftl", map, new File(serviceDir, "build.gradle"));
        TemplateEngine.write("folder/gradle/settings.ftl", map, new File(serviceDir, "settings.gradle"));
    }

    private void copyJava(Map<String, Object> map) {
        TemplateEngine.write("folder/java/Application.ftl", map, new File(serviceDir, "/src/main/java/" + packagePath + "/Application.java"));
        TemplateEngine.write("folder/java/MyBatisConfig.ftl", map, new File(serviceDir, "/src/main/java/" + packagePath + "/MyBatisConfig.java"));
    }

    private void copyProp(Map<String, Object> map) {
        TemplateEngine.write("folder/prop/application.ftl", map, new File(serviceDir, "/src/main/resources/application.yml"));
        TemplateEngine.write("folder/prop/bootstrap.ftl", map, new File(serviceDir, "/src/main/resources/bootstrap.yml"));

    }


    public void createApp(String appName, String packageName, String destPath) {
        this.packageName = packageName;
        this.serviceName = appName;
        this.destDir = new File(destPath);
        this.serviceDir = new File(destDir, serviceName);
        this.packagePath = packageName.replaceAll("\\.", "/");
        Map<String, Object> map = new HashMap<>();
        map.put("packageName", this.packageName);
        map.put("appName", this.serviceName);
        map.put("serviceName", this.serviceName);
        initFolders();
        copyGradle(map);
        copyXml(map);
        copyJava(map);
        copyProp(map);
    }
}
